package com.zking.web.lp;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zking.service.lp.IndexBasedao;
import com.zking.utils.RedisUtil;

@Controller
public class IndexController {
	
	@Autowired
	IndexBasedao ibo;

	@Autowired
	RedisUtil redisUtil;
	
	@RequestMapping("/seelp")
	public String seelp(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		String pnS = request.getParameter("pn");
		String a = "seelp";
		
		int pn = 0;
		int limit = 5;
		try{
			pn = Integer.parseInt(pnS);
		} catch (Exception e) {
			pn = 1;
		}

		if(pn < 1){
			pn = 1;
		}

		int total = (int)ibo.total() ;
		if(total != 0) {
			int pages = total / limit;
			if(total % limit != 0){
				pages++;
			}

			if(pn > pages){
				pn = pages;
			}

			int start = (pn - 1)*limit;//第一个参数
//			limit =start+5; 
			/* List<Object> post= redisUtil.lGet("informationlp", start, limit-1); */
			List<Map<String, Object>> postlp = ibo.sees(start, limit);
			if (!postlp.isEmpty()) {
				request.setAttribute("postlp", postlp);
				request.setAttribute("pn", pn);
				request.setAttribute("limit", limit);
				request.setAttribute("pages", pages);
				request.setAttribute("a", a);
				modelAndView.setViewName("index");
				return "index";
			}else {
				request.setAttribute("msg", "没有查询到数据");
				return "index";
			}
		}else {
			request.setAttribute("msg", "没有查询到数据");
			return "index";
		}
		
	}
	
	@RequestMapping(value = "/typelp")
	public String post1(HttpServletRequest request,String job_type) throws UnsupportedEncodingException {
		ModelAndView modelAndView = new ModelAndView();
		String pnS = request.getParameter("pn");
		String a = "typelp";
		int pn = 0;
		int limit = 5;
		try{
			pn = Integer.parseInt(pnS);
		} catch (Exception e) {
			pn = 1;
		}

		if(pn < 1){
			pn = 1;
		}
		System.out.println(pn);

		int total = (int)ibo.total2(job_type) ;
		if(total != 0) {
			int pages = total / limit;
			if(total % limit != 0){
				pages++;
			}

			if(pn > pages){
				pn = pages;
			}

			int start = (pn - 1)*limit;//第一个参数
			List<Map<String, Object>> postlp = ibo.post1(start,limit, job_type);
			if (!postlp.isEmpty()) {
				request.setAttribute("postlp", postlp);
				request.setAttribute("pn", pn);
				request.setAttribute("limit", limit);
				request.setAttribute("pages", pages);
				request.setAttribute("a", a);
				request.setAttribute("job_type", job_type);
			
				//将整个list集合序列化
//				System.out.println("序列化之前："+postlp);
//				String postlpSerilize = new String(SerializeUtils.serialize(postlp), "ISO-8859-1");
//				System.out.println("序列化之后："+postlpSerilize);
//				Object obj = SerializeUtils.deserialize(postlpSerilize.getBytes("ISO-8859-1"));
//				System.out.println("反序列化之后obj="+(List<Map<String, Object>>)obj);
//				redisUtil.set("postlp", postlpSerilize);
				
//				String s = JSONObject.toJSONString(postlp);
//				List<Map> list = JSONObject.parseArray(s, Map.class);
//				System.out.println("list="+list);
//				
//				redisUtil.set("postlp", JSONObject.toJSONString(postlp));
				return "index";
			}else {
				request.setAttribute("msg", "没有查询到数据");
				return "index";
			}
		}else {
			request.setAttribute("msg", "没有查询到数据");
			return "index";
		}
		
	}
	
	@RequestMapping(value = "/namelp")
	public String post2(HttpServletRequest request,String job_name) throws UnsupportedEncodingException  {
		ModelAndView modelAndView = new ModelAndView();
		String pnS = request.getParameter("pn");
		String a = "namelp";
		int pn = 0;
		int limit = 5;
		try{
			pn = Integer.parseInt(pnS);
		} catch (Exception e) {
			pn = 1;
		}

		if(pn < 1){
			pn = 1;
		}
		System.out.println(pn);

		int total = (int)ibo.total3(job_name) ;
		if(total != 0) {
			int pages = total / limit;
			if(total % limit != 0){
				pages++;
			}

			if(pn > pages){
				pn = pages;
			}

			int start = (pn - 1)*limit;//第一个参数
			List<Map<String, Object>> postlp = ibo.post2(start,limit, job_name);
			if (!postlp.isEmpty()) {
				request.setAttribute("postlp", postlp);
				request.setAttribute("pn", pn);
				request.setAttribute("limit", limit);
				request.setAttribute("pages", pages);
				request.setAttribute("a", a);
				request.setAttribute("job_type", job_name);
			
				return "index";
			}else {
				request.setAttribute("msg", "没有查询到数据");
				return "index";
			}
		}else {
			request.setAttribute("msg", "没有查询到数据");
			return "index";
		}
		
	}
	
	@RequestMapping(value = "/querylp")
	public String inform2(HttpServletRequest request,String job_name) throws UnsupportedEncodingException  {
		System.out.println(job_name);
		ModelAndView modelAndView = new ModelAndView();
		String pnS = request.getParameter("pn");
		String a = "querylp";
		int pn = 0;
		int limit = 5;
		try{
			pn = Integer.parseInt(pnS);
		} catch (Exception e) {
			pn = 1;
		}

		if(pn < 1){
			pn = 1;
		}
		System.out.println(pn);

		int total = (int)ibo.total3(job_name) ;
		if(total != 0) {
			System.out.println(total);
			int pages = total / limit;
			if(total % limit != 0){
				pages++;
			}

			if(pn > pages){
				pn = pages;
			}

			int start = (pn - 1)*limit;//第一个参数
			List<Map<String, Object>> postlp = ibo.inform2(start,limit,job_name);
			if (!postlp.isEmpty()) {
				request.setAttribute("postlp", postlp);
				request.setAttribute("pn", pn);
				request.setAttribute("limit", limit);
				request.setAttribute("pages", pages);
				request.setAttribute("a", a);
				request.setAttribute("job_type", job_name);
			
				return "index";
			}else {
				request.setAttribute("msg", "没有查询到数据");
				return "index";
			}
		}else {
			request.setAttribute("msg", "没有查询到数据");
			return "index";
		}
	}
	
//	@RequestMapping(value = "/seelp")
//	public ModelAndView see(HttpServletRequest request) {
//		ModelAndView modelAndView = new ModelAndView();
//		List<Map<String, Object>> postlp = ibo.see();
//		
//		if(postlp != null) {
//			modelAndView.addObject("postlp", postlp);
//			modelAndView.setViewName("index");
//			
//			System.out.println(postlp);
//		}
//		return modelAndView;
//	}
	
//	@RequestMapping(value = "/typelp")
//	public ModelAndView post1(HttpServletRequest request,String job_types) {
//		System.out.println("job_types="+job_types);
//		ModelAndView modelAndView = new ModelAndView();
//		List<Map<String, Object>> postlp = ibo.post1(job_types);
//		System.out.println(postlp);
//			modelAndView.addObject("postlp", postlp);
//			System.out.println(postlp);
//			modelAndView.setViewName("index");
//		return modelAndView;
//	}
	
//	@RequestMapping(value = "/namelp")
//	public ModelAndView post2(HttpServletRequest request,String job_name) {
//		ModelAndView modelAndView = new ModelAndView();
//		List<Map<String, Object>> postlp = ibo.post2(job_name);
//		if(postlp != null) {
//			modelAndView.addObject("postlp", postlp);
//			System.out.println(postlp);
//			modelAndView.setViewName("index");
//		}
//		return modelAndView;
//	}
//	
//	@RequestMapping(value = "/querylp")
//	public ModelAndView querys(HttpServletRequest request,String job_name) {
//		ModelAndView modelAndView = new ModelAndView();
//		List<Map<String, Object>> postlp = ibo.inform2(job_name);
//		if(postlp != null) {
//			modelAndView.addObject("postlp", postlp);
//			System.out.println(postlp);
//			modelAndView.setViewName("index");
//		}else {
//			modelAndView.setViewName("create");
//		}
//		return modelAndView;
//	}
}
